package com.rambo.weather.mvp;

import com.rambo.baselib.model.weather.WeatherApiResult;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.util.UiExecutor;
import com.rambo.weather.bean.WeatherBean;
import ohos.app.dispatcher.Group;
import ohos.app.dispatcher.TaskDispatcher;

/***********************************************************************
 * <p>@description:
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/18 15:45
 **********************************************************************/
public class WeatherPresenter extends BasePresenter<IWeatherView> {
    WeatherRepository repository = new WeatherRepository();

    public void getWeather(TaskDispatcher taskDispatcher, WeatherBean weatherBean) {
        showLoading("载入中...", weatherBean.isDialogStyle);
        Group group = taskDispatcher.createDispatchGroup();
        taskDispatcher.asyncGroupDispatch(group, new Runnable() {
            @Override
            public void run() {
                weatherBean.nowResult = repository.getWeatherNow(weatherBean.location);
            }
        });
        taskDispatcher.asyncGroupDispatch(group, new Runnable() {
            @Override
            public void run() {
                weatherBean.dayResult = repository.getWeather3d(weatherBean.location);
            }
        });
        taskDispatcher.asyncGroupDispatch(group, new Runnable() {
            @Override
            public void run() {
                weatherBean.warnResult = repository.getWeatherWarn(weatherBean.location);
            }
        });
        taskDispatcher.asyncDispatchBarrier( new Runnable() {
            @Override
            public void run() {
                UiExecutor.getInstance().runOnMainThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!isAttach()) return;
                        getView().getWeatherResult(weatherBean);
                    }
                });
            }
        });
    }
}
